from datetime import datetime
import sqlite3
import time
from turtle import title


class DB():

    def __init__(self):
        self.cx = sqlite3.connect("./init/dytt.db", check_same_thread=False)
        self.cu = self.cx.cursor()

    def read_all(self, sql):
        self.cu.execute(sql)
        # self.cx.commit()
        return self.cu.fetchall()

    def make_list(self, title_key):
        sql = "SELECT shengchan_code FROM code where guomin_code like '{}'".format(title_key)
        # SUBSTRING(@YourField,charindex(@Keyword,@YourField) + LEN(@Keyword), LEN(@YourField) )
        # print(sql)
        self.cu.execute(sql)
        result = self.cu.fetchall()

        return result  # result_list

    def make_contents(self, title_key):
        sql = "SELECT contents FROM list  where code like '{}'".format(title_key)
        # SUBSTRING(@YourField,charindex(@Keyword,@YourField) + LEN(@Keyword), LEN(@YourField) )
        # print(sql)
        self.cu.execute(sql)
        result = self.cu.fetchall()
        return result  # result_list

    def db_search_code(self, code):
        sql = "SELECT code, name FROM list  where code like '%{}%'".format(code)

        self.cu.execute(sql)
        result = self.cu.fetchall()
        return result  # result_list

    def db_search_name(self, name):
        sql = "SELECT code, name FROM list  where name like '%{}%'".format(name)

        self.cu.execute(sql)
        result = self.cu.fetchall()
        return result  # result_list

    def db_search_contents(self, contents):
        sql = "SELECT code, name FROM list  where contents like '%{}%'".format(contents)

        self.cu.execute(sql)
        result = self.cu.fetchall()
        return result  # result_list

    def execute_it(self, sql):
        self.cu.execute(sql)
        # self.cx.commit()

    def close_conn(self):
        self.cx.close()

    def commit_it(self):
        self.cx.commit()


if __name__ == '__main__':
    D = DB()
    result = D.make_list("050")
    print("result", result)
